<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--通用查询-->
      <queryHeader :permission="permission" />

      <crudOperation :permission="permission" />
    </div>

    <el-row :gutter="15">
      <!--角色管理-->
      <el-col :xs="24" :sm="24" :md="16" :lg="16" :xl="17" style="margin-bottom: 10px">
        <el-card class="box-card" shadow="never">
          <div slot="header" class="clearfix">
            <span class="role-span">角色列表</span>
          </div>
          <el-table ref="table" v-loading="crud.loading" highlight-current-row style="width: 100%;" :data="crud.data" @selection-change="crud.selectionChangeHandler" @current-change="handleCurrentChange">
            <el-table-column type="selection" width="55" />

            <el-table-column v-if="columns.visible('code')" prop="code" label="编码" />
            <el-table-column v-if="columns.visible('name')" prop="name" label="名称" />
            <el-table-column label="操作" width="130px" align="center" fixed="right">
              <template slot-scope="scope">
                <udOperation :data="scope.row" :permission="permission" />
              </template>
            </el-table-column>
          </el-table>

          <!--分页组件-->
          <pagination />
        </el-card>
      </el-col>
      <!-- 菜单授权 -->
      <el-col :xs="24" :sm="24" :md="8" :lg="8" :xl="7">
        <el-card class="box-card" shadow="never">
          <div slot="header" class="clearfix">
            <el-tooltip class="item" effect="dark" content="选择指定角色分配菜单" placement="top">
              <span class="role-span">菜单分配</span>
            </el-tooltip>
            <el-button :disabled="!showButton" :loading="menuLoading" icon="el-icon-check" size="mini" style="float: right; padding: 6px 9px" type="primary" @click="saveMenu">保存</el-button>
          </div>
          <el-tree ref="menu" :data="menus" :default-checked-keys="menuIds" :props="defaultProps" check-strictly accordion show-checkbox node-key="resourceId" />
        </el-card>
      </el-col>
    </el-row>

    <!--表单渲染-->
    <editForm />
  </div>
</template>

<script>
import crudRoles, { getRoleMenus } from '@/api/system/auth/role/role'
import editForm from './module/editForm'
import queryHeader from './module/queryHeader'
import { getMenusTree } from '@/api/system/auth/menu/menu'
import CRUD, { presenter } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

// crud交由presenter持有
const defaultCrud = CRUD({ title: '角色', url: 'auth/role/list/page', crudMethod: { ...crudRoles }})

export default {
  name: 'Role',
  components: { pagination, crudOperation, udOperation, editForm, queryHeader },
  mixins: [presenter(defaultCrud)],
  data() {
    return {
      defaultProps: { children: 'childs', label: 'name' },
      currentId: 0, menuLoading: false, showButton: false,
      menus: [], menuIds: [],
      permission: {
        add: [],
        edit: [],
        del: []
      }
    }
  },
  created() {
    this.getMenus()
    this.$nextTick(() => {
      this.crud.toQuery()
    })
  },
  methods: {
    [CRUD.HOOK.afterRefresh]() {
      this.$refs.menu.setCheckedKeys([])
    },
    // 获取所有菜单
    getMenus() {
      getMenusTree().then(res => {
        this.menus = res
      })
    },
    // 触发单选
    handleCurrentChange(val) {
      this.showButton = true
      if (val) {
        const _this = this
        // 清空菜单的选中
        this.$refs.menu.setCheckedKeys([])
        // 保存当前的角色id
        this.currentId = val.id
        // 初始化
        this.menuIds = []
        getRoleMenus(this.currentId).then((data) => {
          _this.$refs.menu.setCheckedNodes(data)
        })
      }
    },
    // 保存菜单
    saveMenu() {
      this.menuLoading = true
      const menuSources = []
      // 得到半选的父节点数据，保存起来
      this.$refs.menu.getHalfCheckedNodes().forEach(function(data, index) {
        menuSources.push(data)
      })
      // 得到已选中的 key 值
      this.$refs.menu.getCheckedKeys().forEach(function(data, index) {
        menuSources.push(data)
      })
      crudRoles.roleBindPms(this.currentId, menuSources).then(res => {
        this.crud.notify('保存成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.menuLoading = false
        this.update()
      }).catch(() => {
        this.menuLoading = false
      })
    },
    // 改变数据
    update() {
      // 无刷新更新 表格数据
      crudRoles.get(this.currentId).then(res => {
        for (let i = 0; i < this.crud.data.length; i++) {
          if (res.id === this.crud.data[i].id) {
            this.crud.data[i] = res
            break
          }
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
  .role-span {
    font-weight: bold;color: #303133;
    font-size: 15px;
  }
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
